from django.http import JsonResponse
from rest_framework.pagination import PageNumberPagination

class StandardPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100



class CustomResponse:
    @staticmethod
    def success(data=None, message='请求成功', status_code=200):
        return JsonResponse({
            "statusCode": status_code,
            "message": message,
            "data": data
        }, json_dumps_params={'ensure_ascii': False})

    @staticmethod
    def error(data=None, message='请求失败', status_code=400):
        return JsonResponse({
            "statusCode": status_code,
            "message": message,
            "data": data
        }, json_dumps_params={'ensure_ascii': False})


class CustomPagination(PageNumberPagination):
    page_size = 10
    #允许前端控制每页数量
    page_size_query_param = 'page_size'
    max_page_size = 100
    def get_paginated_data(self, data):
        data = {
            "total": self.page.paginator,
            "page": self.page.number,
            "pages":self.page.paginator.num_pages,
            "page_size": self.get_page_size(self.request),
            "list":data
        }
        return data

    def get_data(self,data,request):
        data = self.paginate_queryset(data,request)
        data = self.get_paginated_response(data)
        return data.data









